package com.forlove.app.qselect.answer.widght;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.forlove.app.R;
import com.forlove.app.common.utils.DensityUtils;
import com.forlove.app.common.utils.ShareUtils;
import com.forlove.app.common.utils.StatusBarCompat;
import com.forlove.app.common.utils.StringUtils;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.lang.ref.WeakReference;

/**
 * Created by yjl on 2018/1/12.
 */

public class ShareAnswerDetailDialog extends Dialog implements View.OnClickListener {
    private Window mWindow;
    private Activity mContext;
    private ImageView image_share_detail;
    private Bitmap imageBitmap;
    private FrameLayout view_go_back;
    private final int show_image = 100086;//显示图片
    private TextView view_share_weixin; //分享微信
    private TextView view_share_pyq;//分享朋友圈
    private TextView view_share_qq;//分享qq
    private TextView view_share_qzone;//qq空间
    private TextView view_share_sina;//分享微博
    private UMShareListener umShareListener;

    public ShareAnswerDetailDialog(@NonNull Activity activity) {
//        super(context, R.style.DialogCandidateStyle);
        super(activity, R.style.DialogCandidateStyle);
        WeakReference<Activity> contextWeakReference = new WeakReference<>(activity);
        mContext = contextWeakReference.get();
        setContentView(R.layout.dialog_share_answer);
        initWindow();
        image_share_detail = findViewById(R.id.image_share_detail);
        view_go_back = findViewById(R.id.view_go_back);
        view_go_back.setOnClickListener(this);
        view_share_weixin = findViewById(R.id.view_share_weixin);
        view_share_weixin.setOnClickListener(this);
        view_share_pyq = findViewById(R.id.view_share_pyq);
        view_share_pyq.setOnClickListener(this);
        view_share_qq = findViewById(R.id.view_share_qq);
        view_share_qq.setOnClickListener(this);
        view_share_qzone = findViewById(R.id.view_share_qzone);
        view_share_qzone.setOnClickListener(this);
        view_share_sina = findViewById(R.id.view_share_sina);
        view_share_sina.setOnClickListener(this);
        umShareListener = new UMShareListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {

            }

            @Override
            public void onResult(SHARE_MEDIA share_media) {
                Toast.makeText(mContext, ShareUtils.getMedia(share_media) + "分享成功!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                if (throwable.toString().contains("2008")) {
                    Toast.makeText(mContext, "没有安装相关应用", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(mContext, "分享" + ShareUtils.getMedia(share_media) + "失败!", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {
                Toast.makeText(mContext, "分享" + ShareUtils.getMedia(share_media) + "取消!", Toast.LENGTH_SHORT).show();
            }
        };
    }

    private void initWindow() {
        mWindow = getWindow();
//        mWindow.setWindowAnimations(R.style.QYQ_Dialog_Animation_RightInRightOut);
        WindowManager.LayoutParams params = mWindow.getAttributes();
        params.gravity = Gravity.BOTTOM;
        WindowManager windowManager = ((Activity) mContext).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        params.width = (int) (display.getWidth());
        mWindow.setAttributes(params);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_go_back:
                dismiss();
                break;
            case R.id.view_share_weixin:
                ShareUtils.toShareImage(SHARE_MEDIA.WEIXIN, mContext, imageBitmap, umShareListener);
                break;
            case R.id.view_share_pyq:
                ShareUtils.toShareImage(SHARE_MEDIA.WEIXIN_CIRCLE, mContext, imageBitmap, umShareListener);
                break;
            case R.id.view_share_qq:
                ShareUtils.toShareImage(SHARE_MEDIA.QQ, mContext, imageBitmap, umShareListener);
                break;
            case R.id.view_share_qzone:
                ShareUtils.toShareImage(SHARE_MEDIA.QZONE, mContext, imageBitmap, umShareListener);
                break;
            case R.id.view_share_sina:
                ShareUtils.toShareImage(SHARE_MEDIA.SINA, mContext, imageBitmap, umShareListener);
                break;
        }
    }

    public void showShortView(Bitmap bitmap) {
        imageBitmap = bitmap;
        shotImageView();
    }

    private String imageLocalUrl;

    public void showShortView(String imageUrl) {
        imageLocalUrl = imageUrl;
        shotImageView();
    }

    private HandlerThread shotImageThread;
    private Handler shotHandler;
    private Handler showBitmapHandler;

    private void shotImageView() {
        if (shotImageThread == null) {
            shotImageThread = new HandlerThread("shotImageThread");
            shotImageThread.start();
        }
        if (shotHandler == null) {
            shotHandler = new Handler(shotImageThread.getLooper());
        }
        if (showBitmapHandler == null) {
            showBitmapHandler = new Handler(Looper.getMainLooper()) {
                @Override
                public void handleMessage(Message msg) {
                    if (msg.what == show_image) {
                        image_share_detail.setImageBitmap(imageBitmap);
                    }
                }
            };

        }
        shotHandler.post(new Runnable() {
            @Override
            public void run() {
                if (StringUtils.isNotEmpty(imageLocalUrl)) {
                    imageBitmap = BitmapFactory.decodeFile(imageLocalUrl);
                }
                //如果系统是6.0以下,需要裁剪
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M && imageBitmap != null) {
                    int realHeight = DensityUtils.getScreenH(mContext) - DensityUtils.dip2px(getContext(), 48)
                            - StatusBarCompat.getStatusBarHeight(mContext);
                    imageBitmap = Bitmap.createBitmap(imageBitmap, 0, StatusBarCompat.getStatusBarHeight(mContext),
                            DensityUtils.getScreenW(mContext), realHeight);
                }
                if (showBitmapHandler != null) {
                    showBitmapHandler.sendEmptyMessage(show_image);
                }
            }
        });
    }

    /**
     * 释放资源
     */
    public void clearData() {
        if (shotImageThread != null) {
            shotImageThread.quit();
            shotImageThread = null;
        }
        if (shotHandler != null) {
            shotHandler.removeCallbacksAndMessages(null);
            shotHandler = null;
        }
        if (showBitmapHandler != null) {
            showBitmapHandler.removeCallbacksAndMessages(null);
            showBitmapHandler = null;
        }
        if (imageBitmap != null) {
            imageBitmap.recycle();
            imageBitmap = null;
        }
        ShareUtils.clearShare(mContext);
    }
}
